#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteAnswerTemplateProvider : CY.GFive.Core.Providers.DALProvider.IVoteAnswerTemplateProvider
	{
		#region Sql
		private static readonly string SqlSelectVoteAnswerTemplate = "SELECT [Id], [VoteAnswerConten] FROM [VoteAnswerTemplate] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVoteAnswerTemplate = "INSERT INTO [VoteAnswerTemplate]([VoteAnswerConten]) values(@VoteAnswerConten);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVoteAnswerTemplate = "UPDATE [VoteAnswerTemplate] SET [VoteAnswerConten] = @VoteAnswerConten WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVoteAnswerTemplate = "DELETE [VoteAnswerTemplate] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVoteAnswerTemplate = "SELECT [Id], [VoteAnswerConten] FROM [VoteAnswerTemplate]";
		#endregion

		#region IVoteAnswerTemplateProvider Members
		public Core.Business.VoteAnswerTemplate Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVoteAnswerTemplate);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.VoteAnswerTemplate voteAnswerTemplate = new Core.Business.VoteAnswerTemplate();

				if (!reader.IsDBNull(0)) voteAnswerTemplate.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) voteAnswerTemplate.VoteAnswerConten = reader.GetString(1);

				return voteAnswerTemplate;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.VoteAnswerTemplate voteAnswerTemplate)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteAnswerConten", SqlDbType.NVarChar, voteAnswerTemplate.VoteAnswerConten);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVoteAnswerTemplate);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) voteAnswerTemplate.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.VoteAnswerTemplate voteAnswerTemplate)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteAnswerConten", SqlDbType.NVarChar, voteAnswerTemplate.VoteAnswerConten);
			sql.AddParameter("@Id", SqlDbType.Int, voteAnswerTemplate.Id);
			sql.ExecuteSql(SqlUpdateVoteAnswerTemplate);
		}

		public void Delete(Core.Business.VoteAnswerTemplate voteAnswerTemplate)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, voteAnswerTemplate.Id);
			sql.ExecuteSql(SqlDeleteVoteAnswerTemplate);
		}

		public IList<Core.Business.VoteAnswerTemplate> GetAllVoteAnswerTemplate()
		{
			IList<Core.Business.VoteAnswerTemplate> voteAnswerTemplatelist = new List<Core.Business.VoteAnswerTemplate>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVoteAnswerTemplate);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.VoteAnswerTemplate voteAnswerTemplate = new Core.Business.VoteAnswerTemplate();

					if (!reader.IsDBNull(0)) voteAnswerTemplate.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) voteAnswerTemplate.VoteAnswerConten = reader.GetString(1);

					voteAnswerTemplate.MarkOld();
					voteAnswerTemplatelist.Add(voteAnswerTemplate);
				}
				reader.Close();
			}
			return voteAnswerTemplatelist;
		}
		#endregion
	}
}
